from PyQt5.QtWidgets import QWidget, QGridLayout, QLabel

from ClientUi.controls.PieChart import PieChart


class StatisticsWidget(QWidget):
    __resultOKColor = 'rgb(0,254,102)'
    __resultNGColor = 'rgb(255,0,0)'

    def __init__(self):
        super().__init__()
        self.__initUi()

    def __initUi(self):
        grid = QGridLayout()
        lbResult = QLabel('当前结果:')
        lbResult.setMaximumWidth(60)
        lbTotal = QLabel('总计:')
        lbTotal.setMaximumWidth(60)
        lbGood = QLabel('良品:')
        lbGood.setMaximumWidth(60)
        lbRate = QLabel('良率:')
        lbRate.setMaximumWidth(60)
        self._stats_current_result = QLabel()
        self._stats_current_result.setObjectName('LabelResult')
        self._stats_total = QLabel()
        self._stats_good = QLabel()
        self._stats_rate = QLabel()
        self._stats_pie = PieChart()
        grid.addWidget(lbResult, 0, 0)
        grid.addWidget(lbTotal, 1, 0)
        grid.addWidget(lbGood, 2, 0)
        grid.addWidget(lbRate, 3, 0)
        grid.addWidget(self._stats_current_result, 0, 1)
        grid.addWidget(self._stats_total, 1, 1)
        grid.addWidget(self._stats_good, 2, 1)
        grid.addWidget(self._stats_rate, 3, 1)
        grid.addWidget(self._stats_pie, 0, 2, 4, 1)
        grid.setContentsMargins(0, 0, 0, 0)
        grid.setSpacing(0)
        self.setLayout(grid)

    def SetStatistics(self, stats):
        #stats = ['OK', 118, 111, 0]
        #stats[3] = 100*stats[2]/stats[1]
        curResult = stats[0]
        curResult = curResult.upper()
        self._stats_current_result.setText(curResult)
        if curResult == 'OK':
            self._stats_current_result.setStyleSheet(f"color: {self.__resultOKColor};")
        else:
            self._stats_current_result.setStyleSheet(f"color: {self.__resultNGColor};")
        self._stats_total.setText(f'{stats[1]}')
        self._stats_good.setText(f'{stats[2]}')
        self._stats_rate.setText(f'{stats[3]:.1f}%')
        self._stats_pie.setValue(stats[3])

    def ClearStatistics(self):
        self._stats_current_result.setText('')
        self._stats_total.setText('')
        self._stats_good.setText('')
        self._stats_rate.setText('')
        self._stats_pie.clear()

